<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="报警污水池" label-width="100px" prop="containerId">
      <el-select v-model="dataForm.containerId" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.containerId"
          :label="item.numberPlate"
          :value="item.containerId">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="报警液位值" label-width="100px" prop="alarmLiquidValue">
      <el-input v-model="dataForm.alarmLiquidValue" placeholder="报警液位值"></el-input>
    </el-form-item>
    <el-form-item label="报警时间" label-width="100px" prop="alarmTime">
      <el-input v-model="dataForm.alarmTime" placeholder="报警时间"></el-input>
    </el-form-item>
    <!-- <el-form-item label="污水池地址" prop="alarmAddress">
      <el-input v-model="dataForm.alarmAddress" placeholder="污水池地址"></el-input>
    </el-form-item> -->
    <el-form-item label="处理状态" label-width="100px" prop="status">
      <el-input v-model="dataForm.status" placeholder="处理状态 0:未处理 1:处理中 2:处理完成"></el-input>
    </el-form-item>
    <el-form-item label="处理详情" label-width="100px" prop="handleDetail">
      <el-input v-model="dataForm.handleDetail" placeholder="处理详情"></el-input>
    </el-form-item>
    <!-- <el-form-item label="报警图片" prop="alarmImg">
      <el-input v-model="dataForm.alarmImg" placeholder="报警图片"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          containerId: '',
          alarmLiquidValue: '',
          alarmTime: '',
          alarmAddress: '',
          status: '',
          handleDetail: '',
          alarmImg: ''
        },
        dataRule: {
          containerId: [
            { required: true, message: '报警污水池id不能为空', trigger: 'blur' }
          ],
          alarmLiquidValue: [
            { required: true, message: '报警液位值不能为空', trigger: 'blur' }
          ],
          alarmTime: [
            { required: true, message: '报警时间不能为空', trigger: 'blur' }
          ],
          alarmAddress: [
            { required: true, message: '报警地址污水池所在地址不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '处理状态 0:未处理 1:处理中 2:处理完成不能为空', trigger: 'blur' }
          ],
          handleDetail: [
            { required: true, message: '处理详情不能为空', trigger: 'blur' }
          ],
          alarmImg: [
            { required: true, message: '报警图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signcontaineralarm/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.containerId = data.signContainerAlarm.containerId
                this.dataForm.alarmLiquidValue = data.signContainerAlarm.alarmLiquidValue
                this.dataForm.alarmTime = data.signContainerAlarm.alarmTime
                this.dataForm.alarmAddress = data.signContainerAlarm.alarmAddress
                this.dataForm.status = data.signContainerAlarm.status
                this.dataForm.handleDetail = data.signContainerAlarm.handleDetail
                this.dataForm.alarmImg = data.signContainerAlarm.alarmImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sign/signcontaineralarm/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'containerId': this.dataForm.containerId,
                'alarmLiquidValue': this.dataForm.alarmLiquidValue,
                'alarmTime': this.dataForm.alarmTime,
                'alarmAddress': this.dataForm.alarmAddress,
                'status': this.dataForm.status,
                'handleDetail': this.dataForm.handleDetail,
                'alarmImg': this.dataForm.alarmImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
